package b1.mobile.blackberry.screen;

import org.json.me.JSONObject;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.VerticalFieldManager;
import b1.mobile.blackberry.dao.ActivityDAO;
import b1.mobile.blackberry.dao.ActivityEntity;
import b1.mobile.blackberry.dao.DataAccessDelegate;
import b1.mobile.blackberry.io.http.ResponseParserHelper;
import b1.mobile.blackberry.io.http.Serviceworker;
import b1.mobile.blackberry.ui.component.ListGroupItemField;
import b1.mobile.blackberry.ui.component.ListGroupItemManager;
import b1.mobile.blackberry.ui.component.util.FieldFactory;
import b1.mobile.blackberry.ui.component.util.ThemeUtil;
import b1.mobile.blackberry.util.JSONUtil;

public class ActivityDetailScreen extends BaseFunctionScreen implements DataAccessDelegate {
	private String id;
	private Manager bgMgr = new VerticalFieldManager(Field.USE_ALL_WIDTH);
	private ListGroupItemManager listMgr;
	private ActivityEntity _ety;
	
	public ActivityDetailScreen(String id) {
		setTitle(FieldFactory.makeScreenTitle("Activity Detail"));
		ThemeUtil.decorateScreen(this);
		
		bgMgr.setPadding(10, 0, 10, 0);
		
		listMgr = new ListGroupItemManager();
		this.id = id;
		
		bgMgr.add(listMgr);
		add(bgMgr);
	}
	
	protected void onUiEngineAttached(boolean attached) {
		if (attached){			 
			 ActivityDAO dao = new ActivityDAO(this);
			 dao.get(this.id);
		}
	}

	public void onDataReady(Serviceworker worker, Object data) {
		super.onDataReady(worker, data);
		
		if(worker.responseCode == 200){
			this._ety = (ActivityEntity)data;
			
			UiApplication.getUiApplication().invokeLater(new Runnable(){
				public void run(){
					bind();
				}
			});
		}
	}

	private void bind() {		
		listMgr.add(new ListGroupItemField("Activity", _ety.getType(), false));
		listMgr.add(new ListGroupItemField("Contact", _ety.getStringProperty(ActivityEntity.JK_CONTACT), false));
		// TODO: adding listener to link to BP detail
		ListGroupItemField bpItem = new ListGroupItemField("BP", _ety.getStringProperty(ActivityEntity.JK_CARDNAME), true);
		bpItem.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				onBP();
			}
		});
		listMgr.add(bpItem);
		
		listMgr.add(new ListGroupItemField("Remarks", _ety.getStringProperty(ActivityEntity.JK_REMARKS), false));
		listMgr.add(new ListGroupItemField("Content", _ety.getStringProperty(ActivityEntity.JK_CONTENT), false));
		
		listMgr.add(new ListGroupItemField("Document", _ety.getDocumentDesc(), false));
		listMgr.add(new ListGroupItemField("Handled by", _ety.getStringProperty(ActivityEntity.JK_BY), false));
		
		listMgr.add(new ListGroupItemField("Start Time", _ety.getStartTime(), false));
		listMgr.add(new ListGroupItemField("End Time", _ety.getStartTime(), false));
		
		String type = _ety.getTypeCode();
		if(type.equals("M") || type.equals("C")){
			listMgr.add(new ListGroupItemField("Duration", _ety.getDuration(), false));
		} else if(type.equals("T")) {
			listMgr.add(new ListGroupItemField("Status", _ety.getStringProperty(ActivityEntity.JK_STATUS), false));
		}
	}

	protected void onBP() {
		String bpCode = _ety.getBPCode();
		if(bpCode != null){
			UiApplication.getUiApplication().pushScreen(new BPInfoScreen(bpCode));
		}
	}
}
